/*
 * Copyright 2010 Assert Developments
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package be.ad.coffeebreak.validators;

import be.ad.coffeebreak.domain.User;
import be.ad.coffeebreak.exceptions.ValidationException;
import be.ad.coffeebreak.validation.Validator;

import static be.ad.coffeebreak.validation.ValidationUtils.*;

/**
 * A user validator.
 *
 * @author Stefan Bangels
 */
public class UserValidator implements Validator<User> {

    /**
     * All known languages.
     */
    public static final String[] LANGUAGES = new String[]{"en", "nl"};

    /**
     * All known countries.
     */
    public static final String[] COUNTRIES = new String[]{"BE", "NL", "US"};

    /**
     * All known languages.
     */
    public static final String[] TIMEZONES = new String[]{"GMT-2", "GMT-1", "GMT+0", "GMT+1", "GMT+2"};

    /**
     * All known themes.
     */
    public static final String[] THEMES = new String[]{"default", "test"};


    public void validate(Object target) throws ValidationException {
        User user = (User) target;
        rejectIfEmptyOrWhitespace(user.getEmail(), "account.email.required");
        rejectIfEmptyOrWhitespace(user.getName(), "account.name.required");
        rejectIfEmptyOrWhitespace(user.getCountry(), "account.country.required");
        rejectIfEmptyOrWhitespace(user.getLanguage(), "account.language.required");
        rejectIfEmptyOrWhitespace(user.getTimeZone(), "account.timezone.required");
        rejectIfEmptyOrWhitespace(user.getTheme(), "account.theme.required");
        rejectIfTooLong(user.getName(), 80, "account.name.too.long");
        rejectIfTooLong(user.getEmail(), 80, "account.email.too.long");
        rejectIfTooLong(user.getDescription(), 160, "account.description.too.long");
        rejectIfTooLong(user.getCountry(), 3, "account.country.too.long");
        rejectIfTooLong(user.getLanguage(), 3, "account.language.too.long");
        rejectIfTooLong(user.getTimeZone(), 80, "account.timezone.too.long");
        rejectIfTooLong(user.getTheme(), 25, "account.theme.too.long");
        rejectIfNotEmail(user.getEmail(), "account.email.invalid");
        rejectIfNotInValues(user.getCountry(), COUNTRIES, "account.country.invalid");
        rejectIfNotInValues(user.getLanguage(), LANGUAGES, "account.language.invalid");
        rejectIfNotInValues(user.getTimeZone(), TIMEZONES, "account.timezone.invalid");
        rejectIfNotInValues(user.getTheme(), THEMES, "account.theme.invalid");
    }

}
